<?php 
class MstSupplier extends base_model 
{
	const tablename = 'mstsupplier';
	var	$KdBrg;
	var	$NmBrg;
	var	$BCode;
	var	$TypeBrg;
	var	$UnitId;
	var	$MinStock;
	var	$InitStock;

		
	public function __construct($KdBrg='')
	{					
		if($KdBrg!='')
			$this->LookUp($KdBrg);
		//$this->output->enable_profiler(TRUE);

	}

	function Update()
	{
		
		$this->UpdateBy=$this->loginuser['uname'];
		$this->UpdateDate=date('Y-m-d H:i:s');		
		$this->db->where('KdBrg', $this->KdBrg);
		return $this->db->update($this->db->dbprefix($this::tablename), $this); 
	}
	
	function Create($id)
	{	
		$this->CreatedBy=$this->loginuser['uname'];
		$this->Created=date('Y-m-d H:i:s');
		return $this->db->insert($this->db->dbprefix($this::tablename), $this); 
	}

	function LookUp($kdbrng)
	{	
		$arw= $this->Inquiry($kdbrng);	
		
		if(count($arw)>0){				
				$rw=$arw[0];
				$this->KdBrg =$rw['KdBrg'];
				$this->NmBrg =$rw['NmBrg'];
				$this->BCode =$rw['BCode'];
				$this->TypeBrg=$rw['TypeBrg'];
				$this->UnitId=$rw['UnitId'];
				$this->MinStock=$rw['MinStock'];
				$this->InitStock=$rw['InitStock'];
				$this->parentmap($rw);
		}
		else{
			return false;
		}
	}

	function Delete($id)
	{
		$this->db->where_in('KdBrg', $id);
		return $this->db->delete($this->db->dbprefix($this::tablename)); 		
	}	

	public function Inquiry($kdbrg,$bcode=false,$nmbrg=false,$order=false)
	{	
	  	
	$sql= "select a.KdBrg,a.NmBrg,a.BCode,a.TypeBrg,a.UnitId,a.MinStock,a.InitStock"
		  .",b.lkpval as TypeBrgDsc "
		  .",c.lkpval as UnitIdDsc "
		  ."from ".$this->db->dbprefix($this::tablename)." a "
		  ."left join " . $this->db->dbprefix($this->config->item('lookuptable')) . " b on a.TypeBrg=b.lkpkey and b.namespace='typebrg' "
		  ."left join " . $this->db->dbprefix($this->config->item('lookuptable')) . " c on a.UnitId=c.lkpkey  and c.namespace='unit' ";
	
	$where="where 1=1 ";	

		if($kdbrg)			
		 $where.="and a.KdBrg='".$kdbrg."' ";
		
		
		if($bcode)
		$where.="and a.BCode='".$bcode."' ";	 	

		
		if($nmbrg)
		$where.="and a.NmBrg like '%".$nmbrg."%' ";				
						
		$query = $this->db->query($sql.$where);
			
		return $query->result_array();		

	} 
}

?>



